package com.cheneylo.cmp.controller;

import com.cheneylo.cmp.DTO.ProvinceRequestDTO;

import com.cheneylo.cmp.mapper.ZhiyuanMapper;
import com.cheneylo.cmp.pojo.Zhiyuan;
import com.cheneylo.cmp.service.ZhiyuanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/gaokao")
public class ZhiyuanController {
    @Autowired
    private ZhiyuanMapper zhiyuanMapper;
    @Autowired
    private ZhiyuanService zhiyuanService;
    @GetMapping("/findAll")
    @CrossOrigin(origins = "*")
    public List<Zhiyuan> findAll() {
        return zhiyuanMapper.selectList(null);
    }
    @PostMapping("/selectByProvince")
    @CrossOrigin(origins = "*")
    public List<Zhiyuan> selectByProvince(@RequestBody ProvinceRequestDTO request/*利用DTO来获取province值*/) {
        String province = request.getProvince();
        return zhiyuanMapper.selectByProvince(province);
    }
    //查询major
    @GetMapping("/selectByMajor")
    @CrossOrigin(origins = "*")
    public ResponseEntity<List<Zhiyuan>> selectByMajor(@RequestParam(value = "major", required = false, defaultValue = "") String major) {
        List<Zhiyuan> zhiyuan = zhiyuanMapper.selectByMajor(major);
        if (zhiyuan.isEmpty()) {
            // 如果没有找到匹配的课程，可以返回一个空的响应体或特定的状态码
            return ResponseEntity.noContent().build();
        }
        return ResponseEntity.ok(zhiyuan);
    }
    //同时查询专业和大学
    @GetMapping("/selectByMajorAndUniversity")
    @CrossOrigin(origins = "*")
    public ResponseEntity<List<Zhiyuan>> selectByMajorAndUniversity(@RequestParam(value = "major", required = false, defaultValue = "") String major,@RequestParam(value = "university", required = false, defaultValue = "") String university) {
        List<Zhiyuan> zhiyuan = zhiyuanMapper.selectByMajorAndUniversity(major,university);
        if (zhiyuan.isEmpty()) {
            // 如果没有找到匹配的课程，可以返回一个空的响应体或特定的状态码
            return ResponseEntity.noContent().build();
        }
        return ResponseEntity.ok(zhiyuan);
    }
    @PostMapping("/generateStrategy")
    @CrossOrigin(origins = "*")
    public List<String> generateStrategy(@RequestParam("studentRank") int studentRank)
    {
        List<String> strategies = new ArrayList<>();
         List<Zhiyuan> zhiyuan = zhiyuanMapper.findAll();
         for(Zhiyuan z : zhiyuan)
         {
             if (studentRank<4000)
             {
                 if ((studentRank-z.getLowestRank())>1&&(studentRank-z.getLowestRank())<1000)
                     strategies.add(z.getUniversity()+"-"+z.getMajor()+"-"+"可冲刺");
                 else if ((z.getLowestRank()-studentRank)>10&&(z.getLowestRank()-studentRank)<800)
                     strategies.add(z.getUniversity()+"-"+z.getMajor()+"-"+"可稳妥");
                 else if ((z.getLowestRank()-studentRank)>900)
                     strategies.add(z.getUniversity()+"-"+z.getMajor()+"-"+"可保底");
                 else strategies.add(" ");
             }

             if (studentRank<10000&&studentRank>4001)
             {
                 if ((studentRank-z.getLowestRank())>1&&(studentRank-z.getLowestRank())<1500)
                     strategies.add(z.getUniversity()+"-"+z.getMajor()+"-"+"可冲刺");
                 else if ((z.getLowestRank()-studentRank)>100&&(z.getLowestRank()-studentRank)<1500)
                     strategies.add(z.getUniversity()+"-"+z.getMajor()+"-"+"可稳妥");
                 else if ((z.getLowestRank()-studentRank)>1200)
                     strategies.add(z.getUniversity()+"-"+z.getMajor()+"-"+"可保底");
                 else strategies.add(" ");
             }
             if (studentRank<30000&&studentRank>10000)
             {
                 if ((studentRank-z.getLowestRank())>1&&(studentRank-z.getLowestRank())<1800)
                     strategies.add(z.getUniversity()+"-"+z.getMajor()+"-"+"可冲刺");
             }
             if (studentRank<20000&&studentRank>10000)
             {
                  if ((z.getLowestRank()-studentRank)>400&&(z.getLowestRank()-studentRank)<2000)
                 strategies.add(z.getUniversity()+"-"+z.getMajor()+"-"+"可稳妥");
                  else if ((z.getLowestRank()-studentRank)>1200)
                 strategies.add(z.getUniversity()+"-"+z.getMajor()+"-"+"可保底");
             }
             if (studentRank<50000&&studentRank>20000)
             {

                 if ((z.getLowestRank()-studentRank)>4000)
                     strategies.add(z.getUniversity()+"-"+z.getMajor()+"-"+"可保底");
             }

             if (studentRank<40000&&studentRank>30000)
             {
                 if ((studentRank-z.getLowestRank())>1&&(studentRank-z.getLowestRank())<2300)
                     strategies.add(z.getUniversity()+"-"+z.getMajor()+"-"+"可冲刺");

             }
             if (studentRank<50000&&studentRank>40000)
             {
                 if ((z.getLowestRank()-studentRank)>1&&(z.getLowestRank()-studentRank)<4000)
                     strategies.add(z.getUniversity()+"-"+z.getMajor()+"-"+"可冲刺");

             }
             if (studentRank<60000&&studentRank>50000)
         {
             if ((studentRank-z.getLowestRank())>1&&(studentRank-z.getLowestRank())<8000)
                 strategies.add(z.getUniversity()+"-"+z.getMajor()+"-"+"可冲刺");

         }
             if (studentRank>60000)
             {
                 if ((studentRank-z.getLowestRank())>1&&(studentRank-z.getLowestRank())<11000)
                     strategies.add(z.getUniversity()+"-"+z.getMajor()+"-"+"可冲刺");

             }
             if(studentRank<80000&&studentRank>60000)
             {
                  if((z.getLowestRank()-studentRank)>8000)
                 strategies.add(z.getUniversity()+"-"+z.getMajor()+"-"+"可保底");
             }
             if (studentRank<80000&&studentRank>20000)
             {
                 if ((z.getLowestRank()-studentRank)>700&&(z.getLowestRank()-studentRank)<5000)
                     strategies.add(z.getUniversity()+"-"+z.getMajor()+"-"+"可稳妥");

             }
             if (studentRank>80000)
         {
             if ((z.getLowestRank()-studentRank)>700&&(z.getLowestRank()-studentRank)<1000)
                 strategies.add(z.getUniversity()+"-"+z.getMajor()+"-"+"可稳妥");
             else if ((z.getLowestRank()-studentRank)>12000)
                 strategies.add(z.getUniversity()+"-"+z.getMajor()+"-"+"可保底");
         }

         }
         return strategies;
    }
//    @PostMapping("/selectByMajorAndUniversity")
//    public List<Zhiyuan> selectByMajorAndUniversity(@RequestBody MajorAndUniversityRequestDTO request) {
//        String major = request.getMajor();
//        String university = request.getUniversity();
//        return zhiyuanMapper.selectByMajorAndUniversity(major,university);
//    }
    @PostMapping("/add")
    @CrossOrigin(origins = "*")
    public void add(@RequestBody Zhiyuan zhiyuan) {
         zhiyuanMapper.insert(zhiyuan);
    }
    @PostMapping("/delete")
    @CrossOrigin(origins = "*")
    public void delete(@RequestBody Zhiyuan zhiyuan) {
        zhiyuanMapper.deleteById(zhiyuan);
    }
    @PostMapping("/update")
    @CrossOrigin(origins = "*")
    public void update(@RequestBody Zhiyuan zhiyuan) {
        zhiyuanMapper.updateById(zhiyuan);
    }
}
